# -*- coding: utf-8 -*-

"""
@Author: 芮芪懿 QQ1849433491
@Project->File: pygame_train -> 8飞机发射子弹.py
@IDE：PyCharm 2020.1
@File: 8飞机发射子弹.py
@Datetime: 2021/7/8 23:40
"""

import pygame
import random
from resource_path import *
from pygame import Rect

def init_pygame():
    """
    初始化pygame
    :return:
    """
    pygame.init()
    pygame.mixer.init()
    pass


def update_screen():
    """
    update 更新屏幕显示背景图片
    :return:
    """
    pygame.display.update()
    pass


def load_image_resource(path=""):
    """
    加载图片资源
    :param path: 图片路径
    :return: 图片资源
    """
    return pygame.image.load(path)
    pass


def load_sound_resource(path=""):
    """
    加载音频资源
    :param path: 音频路径
    :type path:str
    :return: 音频对象
    """
    return pygame.mixer.Sound(path)
    pass


def play_cycle_bgm():
    """
    循环播放背景音乐
    :return:
    """
    pygame.mixer.music.load(BGM_SOUND_PATH)
    pygame.mixer.music.set_volume(.6)
    pygame.mixer.music.play(-1)
    pass


    

init_pygame()
CREATE_ENEMY_EVENT = pygame.USEREVENT
# 4. 设置定时器事件 - 创建敌机　1s
pygame.time.set_timer(CREATE_ENEMY_EVENT, 1500)
bgImage = pygame.image.load(bgImagePath)  # 加载背景图像数据
player1Image = load_image_resource(player1ImagePath)  # 加载玩家飞机图像数据
player2Image = load_image_resource(player2ImagePath)  # 加载玩家飞机2图像数据
bulletImage = load_image_resource(BULLET_IMAGE_PATH)  # 加载子弹图像数据
enenyImage = load_image_resource(ENEMY_IMAGE_PATH)  # 加载敌方图像数据
bgImageRect = bgImage.get_rect()  # 获取背景矩形
bgImageWidth = bgImageRect.width  # 获取图片宽度
bgImageHeight = bgImageRect.height  # 获取图片高度
screen = pygame.display.set_mode((bgImageWidth, bgImageHeight))  # 屏幕对象,宽高素材一致
# blit 绘制图像
screen.blit(bgImage, (0, 0))
screen.blit(bgImage, (0, -bgImageHeight))
update_screen()
play_cycle_bgm()
imageYStartPosition = 0  # 背景 y轴初始位置
imageYStartPosition2 = - bgImageHeight  # 背景2 y轴初始位置
bgImageOffsetY = 4  # 图片每次移动的Y值
framerate = 65  # 帧数
clock = pygame.time.Clock()  # 创建时钟对象
playerCurrOffsetX = 0  # 玩家飞机当前x偏移量
playerCurrRect = None  # 玩家飞机当前的位置矩形
def set_bgImageOffsetYCycle():
    """
    设置动态背景
    :return:
    """
    global imageYStartPosition
    global imageYStartPosition2
    # 增加Y的偏移量,并重新绘制图片
    imageYStartPosition += bgImageOffsetY
    imageYStartPosition2 += bgImageOffsetY
    screen.blit(bgImage, (0, imageYStartPosition))
    screen.blit(bgImage, (0, imageYStartPosition2))
    # 判断偏移量是否超出了背景图片的高度,是->重置偏移量
    if imageYStartPosition >= bgImageHeight or imageYStartPosition2 >= bgImageHeight:
        imageYStartPosition = 0
        imageYStartPosition2 = - bgImageHeight
    pass


def setPlayerMoveCycle():
    global playerCurrOffsetX
    global playerCurrRect
    playerRect = player1Image.get_rect()  # 玩家飞机矩形
    playerWidth = playerRect.width
    playerHeight = playerRect.height
    playerRect2 = pygame.Rect((bgImageRect.centerx - (playerWidth / 2)+playerCurrOffsetX),
                bgImageHeight - 200, playerWidth, playerHeight)  # 重构飞机矩形用以便捷判断边缘
    playerCurrRect = playerRect2
    # 判断是否超出边缘,超出->重新设置值
    if playerRect2.x <= -10:
        playerRect2.x = -10
    if playerRect2.x >= bgImageWidth-playerWidth+10:
        playerRect2.x = bgImageWidth-playerWidth+10
    screen.blit(player1Image, playerRect2)
    update_screen()
    clock.tick(60)
    screen.blit(player2Image, playerRect2)
    # update_screen()
    pass

def player_fire():
    """
    玩家飞机发射子弹
    :return: 
    """
    originalBulletRect =bulletImage.get_rect()
    bulletRect =pygame.Rect(playerCurrRect.centerx - (originalBulletRect.width/2), playerCurrRect.y - originalBulletRect.height - 5, originalBulletRect.width,
                            originalBulletRect.height)
    screen.blit(bulletImage, bulletRect)
    update_screen()
    load_sound_resource(PLAYER_FIRE_SOUND_PATH).play()
    while bulletRect.y >= 0:
        bulletRect.y -= originalBulletRect.height + 20
        screen.blit(bulletImage,bulletRect)
        update_screen()
        if bulletRect.y <= -50:
            break
    pass

def auto_draw_enemy_play():
    enemyNum = random.randint(1, 4)
    enemyMaxX = bgImage.get_rect().width - enenyImage.get_rect().width
    clock.tick(30)
    for i in range(1, enemyNum + 1):
        enemyRect = Rect(random.randint(0, enemyMaxX), 0,
                         enenyImage.get_rect().width, enenyImage.get_rect().height)
        screen.blit(enenyImage, enemyRect)
        update_screen()
    pass
    
if __name__ == '__main__':
    while True:
        """
        游戏动态背景循环
        """
        clock.tick(framerate)  # 设置帧数
        set_bgImageOffsetYCycle()
        setPlayerMoveCycle()
        for event in pygame.event.get():
            # 判断是否退出游戏
            if event.type == pygame.QUIT:
                exit()
                # pygame.quit()
            elif event.type == CREATE_ENEMY_EVENT:
                auto_draw_enemy_play()
        # 捕获键盘事件 按下右键右移 左键反之
        keysPressed = pygame.key.get_pressed()
        if keysPressed[pygame.K_RIGHT]:
            # print("按了右方向键")
            playerCurrOffsetX += 5
        if keysPressed[pygame.K_LEFT]:
            # print("按了左方向键")
            playerCurrOffsetX -= 5
        if keysPressed[pygame.K_SPACE]:
            player_fire()
        update_screen()

